@tool
extends EditorPlugin


const MENU_NAME = "创建 EditorScript 脚本"

const TEST_PATH := "res://src/test"

func _enter_tree() -> void:
	add_tool_menu_item(MENU_NAME, func():
		if not DirAccess.dir_exists_absolute(TEST_PATH):
			DirAccess.make_dir_recursive_absolute(TEST_PATH)
		
		var filepath = ""
		var idx = 0
		while true:
			filepath = TEST_PATH.path_join("editor_script_%02d.gd" % idx)
			if not FileAccess.file_exists(filepath):
				var script = GDScript.new()
				script.source_code = """# %s
@tool
extends EditorScript


func _run():
	pass
	

""" % filepath.get_file()
				ResourceSaver.save(script, filepath)
				await get_tree().process_frame
				get_editor_interface().get_resource_filesystem().scan()
				get_editor_interface().get_file_system_dock().navigate_to_path(filepath)
				script.reload()
				get_editor_interface().edit_script(load(filepath), 7)
				
				break
			idx += 1
		
	)


func _exit_tree() -> void:
	remove_tool_menu_item(MENU_NAME)
